<template>
  <div class="formDiv">
    <el-card class="box-card">
      <div class="back">
        <div>
          <el-button
            class="submission priamry_border"
            style="display: flex; float: right"
            @click="back"
            >返回</el-button
          >
        </div>
      </div>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        clearable
        label-width="200px"
        :rules="rules"
        class="formArea"
      >
        <el-row>
          <el-col :span="12">
            <!-- 头像素材 -->
            <div class="picUpload">
              <el-form-item label="新增并注册发型素材特征：" prop="attIds">
                <div class="upLoadPicBox">
                  <div
                    v-for="(item, index) in attImages"
                    :key="index"
                    class="pictrue"
                    draggable="true"
                  >
                    <img :src="item" />
                    <i
                      v-if="!isDisabled"
                      class="el-icon-error btndel"
                      @click="handleRemove(index)"
                    />
                  </div>
                  <div
                    v-if="attImages != [] && !isDisabled"
                    @click="modalPicTap('2')"
                  >
                    <div class="upLoad">
                      <i class="el-icon-camera cameraIconfont" />
                    </div>
                  </div>
                </div>
              </el-form-item>
            </div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12"
            ><div>
              <el-form-item class="buttons">
                <el-button type="primary" @click="submitForm('ruleForm')"
                  >确认</el-button
                >
                <el-button @click="cancel()">取消</el-button>
              </el-form-item>
            </div></el-col
          >
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { addAvatarListApi } from "@/api/avatar";
export default {
  name: "addAi",
  created() {},
  data() {
    return {
      isDisabled: this.$route.params.isDisabled === "1" ? true : false,
      attImages: [], //发型素材集合
      //表单
      ruleForm: {
        attIds: [], //发型素材主键Id标识集合
      },
      rules: {
        //发型素材主键Id标识集合
        attIds: [{ required: true, message: "请选择素材", trigger: "change" }],
      },
    };
  },
  methods: {
    // 上传头像素材图片
    modalPicTap(tit, num) {
      const _this = this;
      if (_this.isDisabled) return;
      _this.$modalUpload(
        function (img) {
          if (tit === "1" && !num) {
            _this.attImages = img[0].sattDir;
          }
          if (tit === "2" && !num) {
            if (img.length >= 10)
              return this.$message.warning("最多选择10张图片！");
            if (img.length + _this.attImages.length > 10)
              return this.$message.warning("最多选择10张图片！");
            img.map((item) => {
              _this.attImages.push(item.sattDir);
              _this.ruleForm.attIds.push(item.attId);
            });
          }
        },
        tit,
        "content"
      );
    },
    //删除选择的头像素材
    handleRemove(i) {
      this.attImages.splice(i, 1);
    },
    //返回上一页
    back() {
      this.$router.go(-1);
    },
    //点击保存后的提交
    submitForm() {
      //兜底校验
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return;
        //通过后发请求
        const res = await addAvatarListApi(this.ruleForm);
        this.$message.success(`上传总数为${res.total}张， 上传成功为${res.success}张，上传失败为${res.fail}张`);
        this.$refs.ruleForm.resetFields();
        this.$router.push("/aimanage/avatarManage/index");
      });
    },
    //取消按钮
    cancel() {
      this.$router.go(-1);
    },
  },
  watch: {
    $route: {
      handler() {
        this.attImages = [];
      },
    },
  },
};
</script>

<style lang="scss">
.formDiv {
  margin-top: 40px;
  margin-left: 30px;
  .back {
    width: 100%;
    height: 40px;
    display: flex;
    justify-content: end;
  }
  .formArea {
    padding: 0 30px;
    .province {
      width: 10vw;
      .el-input__inner {
        width: 10vw;
      }
    }
    .picUpload {
      width: 800px;
      height: 100px;
      .upLoadPicBox {
        display: flex;
        width: 800px;
        height: 70px;
      }
    }
    .pictrue {
      width: 60px;
      height: 60px;
      border: 1px dotted rgba(0, 0, 0, 0.1);
      margin-right: 10px;
      position: relative;
      cursor: pointer;
    }
    .btndel {
      position: absolute;
      z-index: 1;
      width: 20px !important;
      height: 20px !important;
      left: 46px;
      top: -4px;
    }
  }
  .el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-col {
    border-radius: 4px;
    .input-textarea {
      width: 30vw;
    }
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }

  .el-input__inner {
    width: 30vw;
  }
  .el-select .el-select--medium {
    width: 40vw;
  }
  .buttons {
    width: 100%;
    height: 50px;
  }
}
.upload {
  width: 80px;
  height: 80px;
  // display: flex;
  text-align: center;
  line-height: 80px;
  background-color: pink;
  border: 1px solid #fff;
}
</style>